package com.backstage.configuration.log;

import com.backstage.common.annotation.log.Operate;
import com.backstage.common.base.enum_bean.LogType;
import com.backstage.common.uitil.HttpContextUtils;
import com.backstage.common.uitil.IPUtils;
import com.backstage.common.uitil.JsonUtil;
import com.backstage.entity.BaseEntity;
import com.backstage.entity.Log;
import com.backstage.entity.User;
import com.backstage.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;


/**
 * @TODO
 * @Author:阮雪峰
 * @Date：2017/8/4 20:18
 * @pagekage: com.backstage.configuration.log
 * @project: authority
 */
@Aspect
@Component
public class LogAspect {

    @Pointcut("execution(* com.backstage.controller.*.*(..)) && @annotation(Operate) ")
    public void pointcut() {
    }

    @Resource(name = "logService")
    private LogService logService;

    @Around("within(com.backstage.controller.*) && @annotation(operate)")
    public Object doAfterReturning(ProceedingJoinPoint joinPoint, Operate operate) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Log log = new Log();
        log.setOperate(operate.value());
        //请求的方法名
        log.setOperate_method(joinPoint.getTarget().getClass().getName()+"."+signature.getName());

        //参数
        Object[] args = joinPoint.getArgs();
        if(LogType.operate == operate.logType()){
            List<Object> params = new ArrayList<>();
            for (Object arg : args) {
                if(arg instanceof Collection){
                    params.add(arg);
                }else if(arg instanceof BaseEntity){
                    params.add(JsonUtil.objectToMap(arg));
                }
            }
            log.setOperate_param(JsonUtil.listToJsonString(params));
        }

        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        //设置IP地址
        log.setOperate_ip(IPUtils.getIpAddr(request));

        //操作人
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        if(user == null){
            for (Object arg : args) {
                if(arg instanceof User){
                    user = (User)arg;
                }
            }
        }
        log.setOperator(user != null ? user.getUser_name():"");

        Object object = joinPoint.proceed();
        log.setOperate_return(JsonUtil.objectToJsonString(object));

        logService.add(log);
        return object;
    }

}
